<?php
namespace app\article\controller;
use think\Db;
use think\Request;
use app\common\controller\AdminBase;

class ApiAdminArticleField extends AdminBase
{
    protected $beforeActionList = ['start'];
    public function start() {
        $this->itemType = 'article';
        $this->itemName = '内容';
        $this->item = 'Articles';
        $this->itemCategory = 'ArticlesCategory';
        $this->itemContent = 'ArticlesContent';
        $this->itemSearch = 'ArticlesSearch';
        $this->itemSearchRel = 'ArticlesSearchRel';
        $this->itemTagsRel = 'ArticlesTagsRel';
        $this->itemModelNameSpace = 'app\article\model\Articles';
        $this->itemCategoryModelNameSpace = 'app\article\model\ArticlesCategory';
        $this->itemTagsRelModelNameSpace = 'app\article\model\ArticlesTagsRel';
    }
    
    public function itemAdd()
    {
        $title = input('post.title');
        $field = input('post.field');
        $modelName = input('post.modelName');
        $style  = input('style');
        $data  = input('data');
        
        $itemInfo = db('ArticlesField')->where('field',$field)->find();
        $modelInfo = db('ArticlesModel')->where('name',$modelName)->find();
        if ($itemInfo) {
            return jsonError('字段已存在');
        }
        $field = 'diy_' . $field;
        if ($style == 'input' || $style == 'upload' || $style == 'radio' || $style == 'checkbox' || $style == 'select' || $style == 'date') {
            $tempFieldType = ' varchar(255) DEFAULT NULL';
            $fieldType = 'varchar';
        }
        if ($style == 'textarea' || $style == 'edit') {
            $tempFieldType = ' longtext';
            $fieldType = 'longtext';
        }
        if ($style == 'int') {
            $tempFieldType = ' int(11) DEFAULT 0';
            $fieldType = 'int';
        }
        if ($style == 'decimal') {
            $tempFieldType = ' decimal(11,2) DEFAULT 0.00';
            $fieldType = 'decimal';
        }
        try {
        	$sql = 'ALTER TABLE articles ADD ' . $field . $tempFieldType;
            Db::query($sql);
        } catch(\Exception $e) {
            return jsonError('添加失败');
        }
        db('ArticlesField')->insert(array('model_name' => $modelName,'title' => $title,'field' => $field,'type' => $fieldType,'style' => $style,'data' => $data,'sort' => 99));
        return jsonSuccess('操作成功');
    }
    
    public function itemNameEdit()
    {
        $id = input('post.id');
        $title = input('post.title');
        $modelName = input('post.modelName');
        $field  = input('field');
        $fieldNewName  = input('fieldNewName');
        $style  = input('style');
        $data  = input('data');
        
        $itemInfo = db('ArticlesField')->where('id',$id)->find();
     	$field = 'diy_' . $field;
     	$fieldNewName = 'diy_' . $fieldNewName;
        if ($style == 'input' || $style == 'upload' || $style == 'radio' || $style == 'checkbox' || $style == 'select' || $style == 'date') {
            $tempFieldType = ' varchar(255) DEFAULT NULL';
            $fieldType = 'varchar';
        }
        if ($style == 'textarea' || $style == 'edit') {
            $tempFieldType = ' longtext';
            $fieldType = 'longtext';
        }
        if ($style == 'int') {
            $tempFieldType = ' int(11) DEFAULT 0';
            $fieldType = 'int';
        }
        if ($style == 'decimal') {
            $tempFieldType = ' decimal(11,2) DEFAULT 0.00';
            $fieldType = 'decimal';
        }
        db('ArticlesField')->where('id',$id)->update(array('title' => $title,'field' => $fieldNewName,'type' => $fieldType,'style' => $style,'data' => $data));
        try {
     		$sql = 'ALTER TABLE articles CHANGE ' . $field . ' ' . $fieldNewName . $tempFieldType;
        	Db::query($sql);
        } catch(\Exception $e) {
            return jsonError('修改名称失败' . $e);
        }
        return jsonSuccess('操作成功');
    }
	
    public function itemDel()
    {
        $id = input('post.id');
        $modelName = input('post.modelName');
        if (!$id) {
          return jsonError('缺少参数');
        }
        $itemInfo = db('ArticlesField')->where('model_name',$modelName)->where('id',$id)->find();
        if (!$itemInfo) {
            return jsonError('删除项不存在');
        }
        $modelInfo = db('ArticlesModel')->where('id',$mid)->find();
     	$sql = 'ALTER TABLE articles drop ' . $itemInfo['field'];
        Db::query($sql);
        db('ArticlesField')->where('model_name',$modelName)->where('id',$id)->delete();
        return jsonSuccess('操作成功');
    }

    public function itemList()
    {
        $modelName = input('post.modelName');
        if (!$modelName) {
          return jsonError('缺少参数');
        }
        $itemList = db('ArticlesField')->where('model_name',$modelName)->order('sort','asc')->select();
        if ($itemList) {
            foreach ($itemList as $key => $value) {
                $itemList[$key]['data'] = explode(',', $value['data']);
            }
        }
	    return jsonSuccess('',$itemList);
    }
    
}